package com.zsh.wanandroid.ui.activity;

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.zsh.wanandroid.R;
import com.zsh.wanandroid.base.BaseActivity;
import com.zsh.wanandroid.bean.ArticleBean;
import com.zsh.wanandroid.bean.ArticleTags;
import com.zsh.wanandroid.utils.UIUtils;

import me.jessyan.autosize.AutoSize;

/**
 * @author zsh
 * @date 2020/3/20.
 * 功能：文章页面
 */

public class ArticleActivity extends BaseActivity {

    private ImageView ivBack;
    private TextView tvTitle;
    private ImageView ivMore;
    private WebView webView;

    private ProgressDialog dialog;
    private WebViewClient webViewClient;

    private ArticleBean<ArticleTags> data;

    @Override
    public void initPrams(Bundle prams) {
        if (null != prams)
            data = (ArticleBean<ArticleTags>) prams.getSerializable("data");
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_article;
    }

    @Override
    public void initView(View view) {
        ivBack = $(R.id.ivBack);
        tvTitle = $(R.id.tvTitle);
        ivMore = $(R.id.ivMore);
        webView = $(R.id.webView);
    }

    @Override
    public void setListener() {
        ivBack.setOnClickListener(this);
        ivMore.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.ivBack://返回
                back();
                break;

            case R.id.ivMore://更多
                UIUtils.showToast("更多");
                break;
        }
    }

    @Override
    public void doBusiness(Bundle savedInstanceState) {
        //设置标题
        tvTitle.setText(data.getTitle());
        ivMore.setImageResource(R.drawable.ic_more_white_24dp);
        String url = data.getLink();

        dialog = new ProgressDialog(this, R.style.BDAlertDialog);
        dialog.setMessage("网页加载中...");
        //AutoSize.autoConvertDensity(this, , false);
        initWebView(url);
    }

    private void initWebView(String url) {
        boolean isSuccess = false;
        boolean isError = false;
        webViewClient = new WebViewClient(){
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                dialog.show();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                dialog.dismiss();
            }
        };

        webView.setWebViewClient(webViewClient);
        webView.loadUrl(url);
    }

    private void back() {
        this.finish();
    }

    @Override
    public void onBackPressed() {
        back();
    }
}
